AddCSLuaFile("cl_init.lua");
AddCSLuaFile("shared.lua");
include("shared.lua");
SWEP.Sounds = {Shot={Sound("stargateww/stunfire1.mp3")},Deploy=Sound("")}; -- Yes, no deploy sounds :(

function SWEP:Initialize()
	self.BaseClass.Initialize(self)
end

function SWEP:SVPrimaryAttack()
	local p = self.Owner;
	local multiply = 3;
	local aimvector = p:GetAimVector();
	local shootpos = p:GetShootPos();
	local vel = p:GetVelocity();
	local filter = {self.Owner,self.Weapon};
	
	if(p:IsPlayer()) then
		local right = aimvector:Angle():Right();
		local up = aimvector:Angle():Up();
		
		local max = util.QuickTrace(shootpos,right*100,filter).Fraction*100 - 10;
		local trans = right:DotProduct(vel)*right/25
		if(p:Crouching()) then
			multiply = 0.3;
			
			shootpos = shootpos + math.Clamp(15,-10,max)*right - 4*up + trans;
		else
			
			shootpos = shootpos + math.Clamp(23,-10,max)*right - 15*up + trans;
		end
		multiply = multiply*math.Clamp(vel:Length()/500,0.3,3); 
	else
		multiply = 0;
	end
	
	local trace = util.QuickTrace(p:GetShootPos(),16*1024*aimvector,filter);
	if(trace.Hit) then
		aimvector = (trace.HitPos-shootpos):Normalize();
	end

	local e = ents.Create("stun_pulse");
	e:SetPos(shootpos);
	e:Spawn();
	e:SetVelocity(aimvector*3500+VectorRand()*multiply);
	e:SetOwner(p);
	e:SetColor(173,255,106,100);
	p:EmitSound(self.Sounds.Shot[math.random(1,#self.Sounds.Shot)],90,math.random(97,103));
	if(self.Owner:IsPlayer()) then self:TakePrimaryAmmo(1) end;
end
